package com.zg.autoflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zg.autoflow.req.MakeSealReq;
import com.zg.autoflow.service.SettleWorkflowStepInstanceService;
import com.zg.common.core.constant.FlowContants;
import com.zg.common.core.dao.autoflow.dao.SettleBizformDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowStepInstanceDao;
import com.zg.common.core.dao.autoflow.entity.SettleBizform;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowInstance;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowStep;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowStepInstance;
import com.zg.common.core.util.GUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class SettleWorkflowStepInstanceServiceImpl implements SettleWorkflowStepInstanceService {

    private final SettleWorkflowStepInstanceDao settleWorkflowStepInstanceDao;
    private final SettleBizformDao settleBizformDao;


    @Override
    public String createInsCode() {
        return GUIDUtils.getUUID32WithPrefix(FlowContants.FLOWSTEPINS_PRE);
    }

    @Override
    public SettleWorkflowStepInstance createStepInstanceByCfg(SettleWorkflowInstance selectedWorkflowInstance, SettleWorkflowStep settleWorkflowStep) {
        //生成流程当前步骤（环节）
        SettleWorkflowStepInstance stepInstance = new SettleWorkflowStepInstance();
        stepInstance.setInstCode(selectedWorkflowInstance.getInstCode());
        stepInstance.setStepInstCode(createInsCode());
        stepInstance.setFlowCode(selectedWorkflowInstance.getFlowCode());
        stepInstance.setFlowName(selectedWorkflowInstance.getFlowName());
        stepInstance.setStepCode(settleWorkflowStep.getStepCode());
        stepInstance.setStepName(settleWorkflowStep.getStepName());
        stepInstance.setCreateDate(new Date());
        return  stepInstance;
    }

    @Override
    public void makeSeal(MakeSealReq makeSealReq) {
        SettleBizform flowStepForm = settleBizformDao.getOne(new LambdaQueryWrapper<SettleBizform>().eq(SettleBizform::getStepInstCode,makeSealReq.getStepInstCode()));
        flowStepForm.setBizFormId(null);
        flowStepForm.setPdfFilePath(makeSealReq.getImgPath());
        flowStepForm.setState(com.zg.autoflow.enm.FlowContants.SettleBizformState.sealed);
        settleBizformDao.updateById(flowStepForm);
        LambdaUpdateWrapper<SettleWorkflowStepInstance> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SettleWorkflowStepInstance::getIsSeal, FlowContants.OK_TAG);
        updateWrapper.eq(SettleWorkflowStepInstance::getStepInstCode, makeSealReq.getStepInstCode());
        settleWorkflowStepInstanceDao.update(updateWrapper);
    }
}
